package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.CustomerMapper;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.ICustomerService;
import cn.wolfcode.crm.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CustomerServiceImpl implements ICustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    public void delete(Long id) {
        customerMapper.deleteByPrimaryKey(id);
    }

    public void save(Customer customer) {
        //通过工具类 获取shiro认证 当前用过
        Employee employee = UserContext.getCurrentUser();
        //设置录入时间
        customer.setInputTime(new Date());
        //设置该客户的销售人员
        customer.setSeller(employee);
        //设置该客户 的录入人是谁
        customer.setInputuser(employee);
        //插入该客户到数据库
        customerMapper.insert(customer);
    }

    public Customer get(Long id) {
        return customerMapper.selectByPrimaryKey(id);
    }

    public List<Customer> listAll() {
        return customerMapper.selectAll();
    }

    public void update(Customer customer) {
        customerMapper.updateByPrimaryKey(customer);
    }

    @Override
    public PageInfo<Customer> query(QueryObject qo) {

        //按照录入时间 倒序 排序
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), "input_time DESC");
        List<Customer> customers = customerMapper.query(qo);
        return new PageInfo(customers);
    }

    @Override
    public Customer checkTel(String tel) {
        return customerMapper.checkByTel(tel);
    }

    @Override
    public List<Customer> selectForStatus() {
        return customerMapper.selectForStatus();
    }

    @Override
    public void changeStatus(Customer customer) {
        customerMapper.changeStatus(customer.getStatus(), customer.getId());
    }

}
